package net.xi.news.quartz;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.xi.news.dao.LogMapper;
import net.xi.news.pojo.Log;
import net.xi.news.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

@Configuration
public class QuartzTask {

    @Autowired
    private LogService logService;

    @Autowired
    private LogMapper logMapper;

//    每天早上4点创建一个对象
    @Scheduled(cron ="0 0 4 * * ? *")
    public void LogTask(){
        LambdaQueryWrapper<Log> queryWrapper= Wrappers.lambdaQuery();
        Date date = new Date();
        DateFormat df1 = DateFormat.getDateInstance();//日期格式，精确到日
        System.out.println(df1.format(date));
        queryWrapper.eq(Log::getName,df1.format(date));
//        如果网站浏览量为空，
        queryWrapper.eq(Log::getType,1);
        List<Log> logs = logMapper.selectList(queryWrapper);
        if (logs==null||logs.size()==0){
            logService.LogAdd(1);
        }
//        如果大数据浏览量为空
        LambdaQueryWrapper<Log> queryWrapper2= Wrappers.lambdaQuery();
        queryWrapper2.eq(Log::getType,2);
        queryWrapper2.eq(Log::getName,df1.format(date));
        List<Log> logs2 = logMapper.selectList(queryWrapper2);
        if (logs2==null||logs2.size()==0){
            logService.LogAdd(2);
        }
    }
}
